package com.xps.charminguser.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.xps.charminguser.R;
import com.xps.charminguser.utils.AppCommond;
import com.xps.charminguser.utils.SharedPreferencesUtils;
import com.xps.charminguser.view.base.BaseActivity;
import com.xps.charminguser.view.fragment.BillFragment;
import com.xps.charminguser.view.fragment.CompleteFragment;
import com.xps.charminguser.view.fragment.ConductFragment;
import com.xps.charminguser.view.fragment.ListFragment;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by 吾丶说谎者 on 2017/11/21.
 */

public class MainActivity extends BaseActivity {


    public static MainActivity activity;
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.main_container)
    FrameLayout mainContainer;
    @BindView(R.id.tv_main)
    TextView tvMain;
    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    ListFragment listFragment;
    CompleteFragment completeFragment;
    ConductFragment conductFragment;
    BillFragment billFragment;
    @BindView(R.id.actionbar_iv_title)
    ImageView actionbarIvTitle;


    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setData() {
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("美天跑腿");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.home_titletext));
        actionbarIvTitle.setVisibility(View.VISIBLE);
        actionbarIvTitle.setImageResource(R.mipmap.hometitleicon);
        tvMain.setSelected(true);
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        activity = this;
        listFragment = new ListFragment();
        completeFragment = new CompleteFragment();
        conductFragment = new ConductFragment();
        billFragment = new BillFragment();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, listFragment).disallowAddToBackStack().commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, completeFragment).disallowAddToBackStack().show(completeFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, conductFragment).disallowAddToBackStack().show(conductFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, billFragment).disallowAddToBackStack().show(billFragment).commit();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        JPushInterface.setAliasAndTags(MainActivity.this, user_token.substring(0, 10), null, null);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.tv_main, R.id.tv_dynamic, R.id.tv_message, R.id.tv_person})
    public void onViewClicked(View view) {
        tvMain.setSelected(false);
        tvDynamic.setSelected(false);
        tvMessage.setSelected(false);
        tvPerson.setSelected(false);
        switch (view.getId()) {
            case R.id.tv_main:
                tvMain.setSelected(true);
                actionbarTvTitle.setText("美天跑腿");
                actionbarTvTitle.setTextColor(getResources().getColor(R.color.home_titletext));
                actionbarIvTitle.setVisibility(View.VISIBLE);
                actionbarIvTitle.setImageResource(R.mipmap.hometitleicon);
                showFragment(listFragment);
                break;
            case R.id.tv_dynamic:
                tvDynamic.setSelected(true);
                actionbarTvTitle.setText("我的订单");
                actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
                actionbarIvTitle.setVisibility(View.GONE);
                showFragment(completeFragment);
                break;
            case R.id.tv_message:
                tvMessage.setSelected(true);
                actionbarTvTitle.setText("订单列表");
                actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
                actionbarIvTitle.setVisibility(View.GONE);
                showFragment(conductFragment);
                break;
            case R.id.tv_person:
                tvPerson.setSelected(true);
                actionbarTvTitle.setText("我的账单");
                actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
                actionbarIvTitle.setVisibility(View.GONE);
                showFragment(billFragment);
                break;
        }
    }

    public void showFragment(Fragment showFragemt) {
        hideFrags();
        this.getSupportFragmentManager().beginTransaction().disallowAddToBackStack().show(showFragemt).commit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (activityName.equals(getClass().getSimpleName())) {
                completeFragment.getOrder();
                conductFragment.getWaitOrder();
                break;
            }
        }
    }

    public void F5() {
        completeFragment.getOrder();
        conductFragment.getWaitOrder();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unreg();
    }

    void unreg() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    public void hideFrags() {
        listFragment.hide();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
    }
}